*********************************
******define YOUR path***********
*********************************
*for reproduction, please adapt the path accordingly: 
global path = "~/data/"

*******************************************
******install packages, IF necessary*******
*******************************************
*ssc install sutex  
*ssc install reghdfe

***********************************
******mainbody: analysis***********
***********************************

***************************************
******figure 1: descriptives***********
***************************************
cd "$path"
use "BBW_data_annual.dta", clear
scatter lpprotestcode year if lpprotestcode<=.5, msize(*.5) legend(off) mcol(538bs7) || mspline lpprotestcode year if lpprotestcode<=.5, bands(8) lcolor(538r) legend(off) ||, by(country, total row(1)) legend(off)

***************************************
******figure 2: descriptives***********
***************************************
cd "$path"
use "BBW_data_annual.dta", clear

twoway ///
(line percent7 year if country==7 & year>=1986) ///
(line pprotest7 year if country==7 & year>=1986) ///
(line pcapbill7 year if country==7 & year>=1986, lcolor(black) lpattern(shortdash)) ///
, title("Germany") ytitle("%") legend(off) name(des1)

twoway ///
(line percent7 year if country==16 & year>=1983) ///
(line pprotest7 year if country==16 & year>=1983) ///
(line pcapbill7 year if country==16 & year>=1983, lcolor(black) lpattern(shortdash)) ///
, title("Spain") ytitle("%") legend(off) name(des2)

twoway ///
(line percent7 year if country==19 & year>=1980) ///
(line pprotest7 year if country==19 & year>=1980) ///
(line pcapbill7 year if country==19 & year>=1980, lcolor(black) lpattern(shortdash)) ///
, title("United Kingdom") ytitle("%") legend(off) name(des3)

twoway ///
(line percent7 year if country==20 & year>=1974) ///
(line pprotest7 year if country==20 & year>=1974) ///
(line pcapbill7 year if country==20 & year>=1974, lcolor(black) lpattern(shortdash)) ///
, title("United States") ytitle("%") ///
legend(order(1 "mip" 2 "protest" 3 "legislation")rows(1) )  name(des4)

grc1leg des1 des2 des3 des4, legendfrom(des4)
graph drop des*
graph export "environmentdescriptive.eps", as(eps) preview(off) replace


***********************************
*table 2: annually (main findings):
*********************************** 
cd "$path"
use "BBW_data_annual.dta", clear
eststo clear
eststo: reghdfe dpcapbill lpcapbill dpprotestcode dpercent c.lpprotestcode c.lpercent, absorb(country) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent##c.dpprotestcode lpprotestcode c.lpercent, absorb(country) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent c.dpprotestcode c.lpprotestcode##c.lpercent, absorb(country) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent##c.dpprotestcode c.lpprotestcode##c.lpercent, absorb(country) cluster(capissue year) 
*table 1:
estfe est*, labels(country "country FE")
esttab est* using main_OLS.tex, replace ///
indicate(`r(indicate_fe)', labels(\checkmark)) ///
se(3) b(3) nobaselevels label ///
stats(r2 r2_a N, labels("\$ R^2$" "\$ adj.R^2$" "\$ N$")) ///
substitute(_ _)

******************************
*****Figure 3: BY ISSUE*******
******************************
cd "$path"
use "BBW_data_annual.dta", clear
drop new 
decode capissue, gen(new)
encode new, gen(ci)
*bills
eststo clear
forval ci=1/12{
	eststo: reghdfe dpcapbill lpcapbill dpprotestcode dpercent lpprotestcode lpercent if ci==`ci', absorb(country) cluster(year)
}
*
coefplot ///
(est1, mlabels(lpprotestcode = 3 "agriculture") pstyle(p1)) ///
(est2, mlabels(lpprotestcode = 5 "rights") pstyle(p1)) ///
(est3, mlabels(lpprotestcode = 3 "crime ") pstyle(p1)) ///
(est4, mlabels(lpprotestcode = 3 "defense") pstyle(p2)) ///
(est5, mlabels(lpprotestcode = 12 "education ") pstyle(p1)) ///
(est6, mlabels(lpprotestcode = 6 "environment") pstyle(p1)) ///
(est7, mlabels(lpprotestcode = 12 "health") pstyle(p1)) ///
(est8, mlabels(lpprotestcode = 4 "housing") pstyle(p1)) ///
(est9, mlabels(lpprotestcode = 2 "economy") pstyle(p1)) ///
(est10, mlabels(lpprotestcode = 1 "migration") pstyle(p1)) ///
(est11, mlabels(lpprotestcode = 1 "social") pstyle(p3)) ///
(est12, mlabels(lpprotestcode = 1 "unemploy") pstyle(p1)), ///
drop(lpcapbill dpprotestcode dpercent lpercent _cons *.country) vertical ///
legend(off) yline(0) xlabel("") title("The baseline effect of protest per issue") ///
ytitle("Coefficients on {&Delta}legislative agenda{subscript:t}")
*
graph export "Billsbaselineissue.eps", as(eps) preview(off) replace
graph save Graph "Billsbaselineissue.gph", replace 


********************************************
*****Figure 4: amplification by issue*******
********************************************
cd "$path"
use "BBW_data_annual.dta", clear
eststo clear
forval ci=1/12{
	eststo: reghdfe dpcapbill lpcapbill dpprotestcode dpercent c.lpprotestcode##c.lpercent if ci==`ci', absorb(country) cluster(year)
}
*
coefplot ///
(est1, mlabels(c.lpprotestcode#c.lpercent = 1 "agriculture") pstyle(p1)) ///
(est2, mlabels(c.lpprotestcode#c.lpercent = 1 "rights") pstyle(p1)) ///
(est3, mlabels(c.lpprotestcode#c.lpercent = 1 "crime ") pstyle(p1)) ///
(est4, mlabels(c.lpprotestcode#c.lpercent = 5 "defense") pstyle(p1)) ///
(est5, mlabels(c.lpprotestcode#c.lpercent = 1 "education ") pstyle(p3)) ///
(est6, mlabels(c.lpprotestcode#c.lpercent = 5 "environment") pstyle(p1)) ///
(est7, mlabels(c.lpprotestcode#c.lpercent = 1 "health") pstyle(p1)) ///
(est8, mlabels(c.lpprotestcode#c.lpercent = 1 "housing") pstyle(p3)) ///
(est9, mlabels(c.lpprotestcode#c.lpercent = 6 "economy") pstyle(p1)) ///
(est10, mlabels(c.lpprotestcode#c.lpercent = 12 "migration") pstyle(p1)) ///
(est11, mlabels(c.lpprotestcode#c.lpercent = 1 "social") pstyle(p1)) ///
(est12, mlabels(c.lpprotestcode#c.lpercent = 1 "unemploy") pstyle(p3)), ///
drop(lpcapbill dpprotestcode dpercent lpprotestcode lpercent _cons *.country) vertical ///
legend(off) yline(0) xlabel("") title("Interaction protest X public priorities") ///
ytitle("Coefficients on {&Delta}legislative agenda{subscript:t}")
*
graph export "Billsinteractionissue.eps", as(eps) preview(off) replace
graph save Graph "Billsinteractionissue.gph", replace 
*margins of unemployment:
gen yaxis=0
reghdfe dpcapbill lpcapbill dpprotestcode dpercent c.lpprotestcode##c.lpercent if ci==12, absorb(country) cluster(year)
margins, dydx(lpercent) at(lpprotestcode=(0(.1)1))
marginsplot, recast(line) plot1opts(lcolor(538b) lwidth(medium)) ///
recastci(rarea)  ciopts(lpattern(none) lwidth(none) color(538b%20)) ///
 ytitle("Effects on linear prediction") ///
 xtitle("protest{sub:t-1}") xlabel(0(0.2)1) ///
 addplot( ///
scatter yaxis lpprotestcode if ci==12, ///
mcolor(gs9) msize(vsmall) ///
xlabel(0(0.2)0.8) ) legend(off) ///
title("{bf:Marginal effect of public opinion}")
*
graph export "unemploy.pdf", replace
graph save Graph "unemploy.gph", replace 


***********************************
******appendix: analysis***********
***********************************

***********************************
******Table A1 A2: issue overview**
***********************************
cd "$path"
use "BBW_data_annual.dta", clear
by capissue: sutex lpprotestcode 
sutex dpcapbill lpcapbill dpprotestcode dpercent lpprotestcode lpercent


***********************************
******Table A3: biannual   ********
***********************************
cd "$path"
use "BBW_data_halfyear.dta", clear
eststo clear
eststo: reghdfe dpcapbill lpcapbill dpprotestcode dpercent c.lpprotestcode c.lpercent, absorb(country) cluster(capissue hy) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent##c.dpprotestcode lpprotestcode c.lpercent, absorb(country) cluster(capissue hy) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent c.dpprotestcode c.lpprotestcode##c.lpercent, absorb(country) cluster(capissue hy) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent##c.dpprotestcode c.lpprotestcode##c.lpercent, absorb(country) cluster(capissue hy) 
*table:
estfe est*, labels(country "country FE")
esttab est* using half_OLS.tex, replace ///
indicate(`r(indicate_fe)', labels(\checkmark)) ///
se(3) b(3) nobaselevels label ///
stats(r2 r2_a N, labels("\$ R^2$" "\$ adj.R^2$" "\$ N$")) ///
substitute(_ _)

***********************************
******Table A4: biennially ********
***********************************
cd "$path"
use "BBW_data_biennially.dta", clear 
eststo clear
eststo: reghdfe dpcapbill lpcapbill dpprotestcode dpercent c.lpprotestcode c.lpercent, absorb(country) cluster(capissue) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent##c.dpprotestcode lpprotestcode c.lpercent, absorb(country) cluster(capissue) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent c.dpprotestcode c.lpprotestcode##c.lpercent, absorb(country) cluster(capissue) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent##c.dpprotestcode c.lpprotestcode##c.lpercent, absorb(country) cluster(capissue) 
*table:
estfe est*, labels(country "country FE")
esttab est* using biennial_OLS.tex, replace ///
indicate(`r(indicate_fe)', labels(\checkmark)) ///
se(3) b(3) nobaselevels label ///
stats(r2 r2_a N, labels("\$ R^2$" "\$ adj.R^2$" "\$ N$")) ///
substitute(_ _)

********************************************************
******Table A5: different protest specifications********
********************************************************
cd "$path"
use "BBW_data_annual.dta", clear
gen constant=1
eststo clear
eststo: reghdfe dpcapbill lpcapbill c.dpprotestcode##c.polconv dpercent c.lpprotestcode c.lpercent, absorb(constant) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill c.dpprotestcode dpercent c.lpprotestcode##c.polconv c.lpercent, absorb(constant) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill c.dpprotestcode##i.majority dpercent c.lpprotestcode c.lpercent, absorb(country) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill c.dpprotestcode dpercent c.lpprotestcode##i.majority c.lpercent, absorb(country) cluster(capissue year) 
gen dummy_sample=1 if e(sample)
eststo: reghdfe dpcapbill lpcapbill dpercent c.lpercent i.protest_medium i.protest_large if dummy_sample==1, absorb(country) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill dpercent c.lpercent##i.protest_medium c.lpercent##i.protest_large if dummy_sample==1, absorb(country) cluster(capissue year) 
*table:
estfe est*, labels(country "country FE")
esttab est* using robust_OLS.tex, replace ///
indicate(`r(indicate_fe)', labels(\checkmark)) ///
se(3) b(3) nobaselevels label ///
stats(r2 r2_a N, labels("\$ R^2$" "\$ adj.R^2$" "\$ N$")) ///
substitute(_ _)

********************************
******Table A6: controls********
********************************
cd "$path"
use "BBW_data_annual.dta", clear
eststo clear
eststo: reghdfe dpcapbill lpcapbill dpprotestcode dpercent c.lpprotestcode c.lpercent lgovt_rile dgovt_rile gdpgrowth lgdpgrowth lunemployment dunemployment linflation dinflation, absorb(country) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent##c.dpprotestcode lpprotestcode  c.lpercent lgovt_rile dgovt_rile gdpgrowth lgdpgrowth lunemployment dunemployment linflation dinflation, absorb(country) cluster(capissue year)  
eststo: reghdfe dpcapbill lpcapbill c.dpercent c.dpprotestcode c.lpprotestcode##c.lpercent lgovt_rile dgovt_rile gdpgrowth lgdpgrowth lunemployment dunemployment linflation dinflation, absorb(country) cluster(capissue year) 
eststo: reghdfe dpcapbill lpcapbill c.dpercent##c.dpprotestcode c.lpprotestcode##c.lpercent lgovt_rile dgovt_rile gdpgrowth lgdpgrowth lunemployment dunemployment linflation dinflation, absorb(country) cluster(capissue year) 
*table:
estfe est*, labels(country "country FE")
esttab est* using controls_OLS.tex, replace ///
indicate(`r(indicate_fe)', labels(\checkmark)) ///
se(3) b(3) nobaselevels label ///
stats(r2 r2_a N, labels("\$ R^2$" "\$ adj.R^2$" "\$ N$")) ///
substitute(_ _)

********************************
******Table A6: per country*****
********************************
cd "$path"
use "BBW_data_annual.dta", clear
gen constant=1
encode countryname, gen(c)
eststo clear
forval c=1/4{
	eststo: reghdfe dpcapbill lpcapbill dpprotestcode dpercent c.lpprotestcode c.lpercent if c==`c', absorb(constant) cluster(capissue year)
}
*table
estfe est*, labels(country "country FE")
esttab est* using countries_OLS.tex, replace ///
indicate(`r(indicate_fe)', labels(\checkmark)) ///
se(3) b(3) nobaselevels label ///
stats(r2 r2_a N, labels("\$ R^2$" "\$ adj.R^2$" "\$ N$")) ///
substitute(_ _)
*plot
label var dpprotestcode "{&Delta}protest{subscript:t}"
label var dpercent "{&Delta}priorities{subscript:t}"
label var lpprotestcode "protest{subscript:t-1}"
label var lpercent "priorities{subscript:t-1}"
coefplot est1 est2 est3 est4, drop(_cons lpcapbill "constant=0") ///
legend(order(2 "DE {it:N=300}" 4 "ES {it:N=187}" 6 "UK {it:N=372}" 8 "US {it:N=252}")) ///
level(95) xline(0) ///
title("{bf:Legislative Agendas:} cross-country comparison") ///
xtitle("Coefficients on {&Delta}legislative agenda{subscript:t}")
*save
graph save Graph "Billscrosscountry.gph", replace
graph export "Billscrosscountry.eps", as(eps) preview(off) replace 
















